package pl.edu.agh.io.sylvia.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import pl.edu.agh.io.sylvia.entity.Keyword;



public class KeywordDAO extends GenericDAO {

	public List<Keyword> getByMonitorId(int id) {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Keyword> results = new ArrayList<Keyword>();
		
		try {
			statement = connection.prepareStatement("SELECT * FROM KEYWORDS JOIN MONITORS ON KEYWORDS.MONITORS_ID = MONITORS.ID WHERE KEYWORDS.MONITORS_ID = ?");
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			while(resultSet.next()) {
				int keywordId = resultSet.getInt("id");
				String word = resultSet.getString("word");
				results.add(new Keyword(keywordId, word));
			}
			return results;
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}
	
	@Override
	public List<Keyword> getAll() {
		throw new RuntimeException("To be implemented.");
	}

	@Override
	public Keyword getById(int id) {
		throw new RuntimeException("To be implemented.");
	}

}
